/**
 * Created by xinguoren on 15-4-17.
 */
var mongoService = require("./mongoService");
exports.sign = function (query, callback) {
    var data = {
        collection: "user",
        insert:{
            name:query.name,
            password:query.password,
            id:query.id
        }
    };
    var search = {
        collection: "user",
        search: {
            name: query.name
        }
    };
    mongoService.search(search, function (back) {
        if (back instanceof Array && back.length > 0) {
            callback({already: true, error: false});
        } else if (typeof back === "string") {
            callback({error: true});
        } else {
            mongoService.insertMongo(data, function (backi) {
                if (typeof  backi === "string") {
                    callback({already: false, error: true});
                } else {
                    callback({already: false, error: false});
                }
            });
        }
    });

};

exports.login = function (query, callback) {
   var qdata = {
       collection:"user",
       search:{
           name:query.name,
           password:query.password
       }
   };
    mongoService.search(qdata,function(back){
        if(typeof  back ==="string"){
            callback({msg:"服务器出错！",error:true});
        }else if(back instanceof  Array && back.length > 0){
            callback(back);
        }else {
            callback({msg:"登录密码错误！",error:true});
        }
    });
};